package com.achievo.reportserver.servlet;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;

import com.achievo.reportserver.ReportConstant;
import com.achievo.reportserver.ReportDomUtils;
import com.achievo.reportserver.config.ReportConfigStore;
import com.achievo.reportserver.config.ReportDef;

/**
 * 
 * Report Server
 * write at Jan 21, 2010
 * @author <a href="mailto:chinaoryx@gmail.com">Hyman.Zhang (Zhang Haipeng)</a>
 * @version 1.0
 * 
 *
 */
public class ReportInfoServlet extends HttpServlet implements ReportConstant{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ReportInfoServlet.class);

	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReportInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Writer writer = response.getWriter();
		ReportDomUtils domUtil = new ReportDomUtils();
		
		String status = "OK";
		String errorDesc = null;
		String reportId=request.getParameter("_REPORTID");
		if (StringUtils.isEmpty(reportId)) {
			status = "ERROR";
			errorDesc = "no reportId";
		}
		
		ReportDef report = ReportConfigStore.getReport(reportId);
		
		if (report == null) {
			status = "ERROR";
			errorDesc = "reportId(" + reportId + ") not exist!";
		}
		
		try {
			domUtil.fillResponseDoc(writer, report, status, errorDesc);
		} catch (DocumentException e) {
			throw new IOException(e);
		}

		

		
		writer.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
